package com.wing.product.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.enums.ProductEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.request.ProductReviewAddForm;
import com.wing.product.model.response.ProductReviewSearchVo;
import com.wing.product.model.response.ReviewDetailVo;
import com.wing.sell.service.OrderProReviewService;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.1.0
 * @Author hz
 * @Date 2024-08-28 16:27
 **/
@Slf4j
@Validated
@Api(value = "Admin-评审管理", tags = "#/product/productRecordReview")
@RestController("productReviewRecordController")
@RequestMapping("product/admin/productRecordReview")
@RequiredArgsConstructor
public class ProductReviewRecordController {

    @Autowired
    private OrderProReviewService orderProReviewService;

    @ApiOperation(value = "查询评审记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "processId", value = "评审性质(必填)", defaultValue = "1", example = "1", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductReviewSearchVo> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            , @RequestParam(defaultValue = "processId") String processId
            , String reviewPostId
            , String productTypeId
            , String orderStatus
            , String customerId
            , String customerSku
            , String orderId
    ) {

        Page<ProductReviewSearchVo> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("reviewPostId", reviewPostId);
        params.put("productTypeId", productTypeId);
        params.put("orderStatus", orderStatus);
        params.put("customerId", customerId);
        params.put("customerSku", customerSku);
        params.put("orderId", orderId);
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        Page<ProductReviewSearchVo> list = orderProReviewService.getReviewRecordPageBy(pg, params, processId);
        list.getRecords().forEach(info ->
                {
                    if (StringUtils.isNotEmpty(info.getStatus())) {
                        info.setStatusName(ProductEnum.ReviewEmum.getNameByCode(info.getStatus()));
                    }
                    if (StringUtils.isNotEmpty(info.getResult())) {
                        info.setResult(ProductEnum.ReviewResultEmum.getNameByCode(info.getResult()));
                    }
                }
        );

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 添加评审任务
     *
     * @param productReviewAddForm
     * @return
     */
    @ApiOperation(value = "添加评审任务", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProductReviewAddForm.class) ProductReviewAddForm productReviewAddForm) {
        if (orderProReviewService.addReviewRecordInfoBy(productReviewAddForm) > 0) {
            return JsonResult.success("添加任务成功");
        }
        return JsonResult.fail("添加任务失败");
    }

}